package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.List;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.GoodsEntity;
import cn.mumzone.pos.model.MemberEntity;
import cn.mumzone.pos.ui.widgets.shopping.AnimShopButton;
import cn.mumzone.pos.util.GoodsUtil;
import cn.mumzone.pos.util.PopupUtil;

/**
 * Created by qiuzhenhuan on 2019/6/14.
 */

public class ADA_CartGoods extends CommonAdapter<GoodsEntity> {

    private MemberEntity memberEntity;
    private ViewHolder mPopupAnchorHolder;
    private PopupWindow mPopHint;
    private boolean isPopupShowing;

    public ADA_CartGoods(Context context) {
        super(context);
    }

    public void setBindMember(MemberEntity bindMember) {
        memberEntity = bindMember;
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_cart_goods;
    }

    @Override
    protected void convert(ViewHolder holder, final GoodsEntity goodsEntity, final int position) {

        holder.setText(R.id.tv_cart_good_name, goodsEntity.getGoodsName());

        TextView tvOriginPrice = holder.getView(R.id.tv_cart_good_price_origin);
        TextView tvPrice = holder.getView(R.id.tv_cart_good_price);


        // 原价，默认划掉
        tvOriginPrice.setText("￥" + goodsEntity.getPrice());
        tvOriginPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

        String selectPrice = GoodsUtil.getGoodsPriceByMember(memberEntity, goodsEntity);
        tvPrice.setText(StringUtils.getFormattedMoneyWithUnit(selectPrice));
        if (selectPrice.equals(goodsEntity.getPrice())) {
            tvOriginPrice.setVisibility(View.GONE);
        } else {
            tvOriginPrice.setVisibility(View.VISIBLE);
        }

        holder.setText(R.id.tv_cart_good_spec, getGoodSpecString(goodsEntity.getGoodsSpec(), goodsEntity.getUnitName()));

        // 删除条目
        holder.setOnClickListener(R.id.iv_delete, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onGoodsDeleted(position);
                }
            }
        });

        // 商品数量的加减，需要实时回调
        AnimShopButton vCartGoodsAmount = holder.getView(R.id.v_cart_goods_amount);
        if (Integer.parseInt(goodsEntity.getGoodsBatchNum()) == 1) {
            vCartGoodsAmount.setMaxCount(Integer.parseInt(goodsEntity.getGoodsQuantity()));
        }
        vCartGoodsAmount.setCount(StringUtils.isNumber(goodsEntity.getGoodsCount()) ? Integer.parseInt(goodsEntity.getGoodsCount()) : 0);
        vCartGoodsAmount.setOnAddDelListener(new GoodsShopButtonAdapter(mContext) {
            @Override
            protected void onAmountChanged(int count) {
                goodsEntity.setGoodsCount(StringUtils.get(count));
                goodsEntity.setSelectCount(count);

                // 判断当前添加的数量是不是比寄存数量或者赠送数量多，要是多就更新该值

                if (mListener != null) {
                    mListener.onAmountChanged(position);
                }
            }

            @Override
            public void onDelSuccess(int count) {
                goodsEntity.setGoodsCount(StringUtils.get(count));
                goodsEntity.setSelectCount(count);

                // 减少商品数量时，判断当前添加的数量是不是比寄存数量或者赠送数量小，要是多就更新该值
                if (count < Integer.parseInt(goodsEntity.getRegisterCount())) {
                    goodsEntity.setRegisterCount(String.valueOf(count));
                }

                if (count < goodsEntity.getPresentNum()) {
                    goodsEntity.setPresentNum(count);
                }

                if (mListener != null) {
                    mListener.onAmountChanged(position);
                }
            }
        });

        holder.setOnClickListener(R.id.v_item_click, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupUtil.dismissPopup(PopupUtil.OPEN_ORDER_MODIFY_PRICE, mPopHint);
                if (mEditGoodsListener != null) {
                    mEditGoodsListener.onEditGoodsInfo(goodsEntity);
                }
            }
        });

        holder.setOnClickListener(R.id.tv_cart_good_price, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditGoodsListener != null) {
                    mEditGoodsListener.onEditGoodsPrice(goodsEntity);
                }
            }
        });

        holder.setText(R.id.tv_register_save_count, getRegisterAndSaveCount(goodsEntity));

        if (position - 1 == 0) {
            mPopupAnchorHolder = holder;
            showPopupHint(holder.getView(R.id.iv_go_right));
        }
    }

    public int getAnchorViewPosition() {
        if (mPopupAnchorHolder != null) {
            View targetView = mPopupAnchorHolder.getView(R.id.iv_go_right);
            targetView.measure(0, 0);
            int[] location = new int[2];
            targetView.getLocationOnScreen(location);
            return location[1] + targetView.getMeasuredHeight() * 2;
        }

        return -1;
    }

    public boolean isPopupShowing() {
        return isPopupShowing;
    }

    private String getRegisterAndSaveCount(GoodsEntity entity) {
        StringBuilder builder = new StringBuilder();
        if (Integer.parseInt(entity.getRegisterCount()) != 0) {
            builder.append("寄存").append(entity.getRegisterCount());
        }

        if (Integer.parseInt(entity.getRegisterCount()) != 0 && entity.getPresentNum() != 0) {
            builder.append("，");
        }

        if (entity.getPresentNum() != 0) {
            builder.append("赠送").append(entity.getPresentNum());
        }

        return builder.toString();
    }

    public void dismissPopup() {
        if (mPopHint != null) {
            mPopHint.dismiss();
            mPopHint = null;
            isPopupShowing = false;
        }
    }

    public interface OnGoodsAmountChangedListener {
        void onAmountChanged(int position);

        void onGoodsDeleted(int position);
    }

    private OnGoodsAmountChangedListener mListener;

    public void setOnGoodsAmountChangedListener(OnGoodsAmountChangedListener mListener) {
        this.mListener = mListener;
    }

    public interface OnEditGoodsListener {
        void onEditGoodsInfo(GoodsEntity entity);

        void onEditGoodsPrice(GoodsEntity entity);
    }

    private OnEditGoodsListener mEditGoodsListener;

    public void setOnEditGoodsListener(OnEditGoodsListener mEditGoodsListener) {
        this.mEditGoodsListener = mEditGoodsListener;
    }


    private String getGoodSpecString(List<GoodsEntity.GoodsSpecBean> goodsSpec, String unitName) {
        StringBuilder builder = new StringBuilder();
        builder.append("规格：");
        if (!ListUtil.isListEmpty(goodsSpec) || !TextUtils.isEmpty(unitName)) {
            for (int i = 0; i < goodsSpec.size(); i++) {
                GoodsEntity.GoodsSpecBean goodsSpecBean = goodsSpec.get(i);
                builder.append(goodsSpecBean.getValue());
                builder.append(",");
            }
            builder.append(unitName);
        } else {
            builder.append("暂无");
        }
        return builder.toString();
    }

    public void showPopupHint(final View targetView) {
        if (!isPopupShowing) {
            isPopupShowing = true;
            targetView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mPopHint = PopupUtil.showBlackBgPopup(targetView,
                            PopupUtil.OPEN_ORDER_MODIFY_PRICE,
                            "点击修改商品议价、寄存、赠品", PopupUtil.LEFT_BOTTOM);
                }
            }, 200);
        }

    }

}